/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package campussmartsecurity;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JButton;

/**
 *
 * @author brlnt
 */
public class Kalender extends javax.swing.JFrame {

    /**
     * Creates new form Kalender
     */
    public Kalender() {
        initComponents();
        buatHeader();
        setCalendar();
        setLocationRelativeTo(null);
        
    }
    
    JButton button[]=new JButton[50];
    JButton butt[]=new JButton[3];
    int x=0,y=0,c=0,date=0;
    String day="",dayclicked="",month1="",result; 
    Calendar cal=Calendar.getInstance();
    int month=cal.get(Calendar.MONTH);
    int year=2013;
    
    void getYear(){
        year = Integer.parseInt(fTahun.getText());
    }
    
    void getName(int i){
        switch(i){
            case 1: day="Mgu";break;
            case 2: day="Sen";break;
            case 3: day="Sel";break;
            case 4: day="Rab";break;
            case 5: day="Kam";break;
            case 6: day="Jum";break;
            case 7: day="Sab";break;
        }
    }
        
    void buatDate(int y){
        if(c<button.length){
        for(int i=1;i<8;i++){
            date++;
            if(i>1)x+=button[c-1].getWidth();
            else x=0;
            button[c]=new JButton();
            button[c].setSize(55, 30);
            button[c].setLocation(x, y);
            button[c].setBackground(Color.WHITE);
            panel.add(button[c]);
            c++;
        } buatDate(y+button[1].getHeight());
        }
    }
    
    void buatHeader(){
       
        for(int i=1;i<8;i++){
            if(i>1) x+=button[i-1].getWidth();
                button[i]=new JButton();
                button[i].setSize(55, 30);
                button[i].setLocation(x, y);
                button[i].setBackground(Color.GRAY);
                getName(i);
                button[i].setText(day);
                panel.add(button[i]);
                c=i+1;
        }
        buatDate(button[1].getHeight());
        
    }
    
    void getMonth(){
        switch(cBulan.getSelectedIndex()){
            case 0: month=0;break;
            case 1: month=1;break;
            case 2: month=2;break;
            case 3: month=3;break;
            case 4: month=4;break;
            case 5: month=5;break;
            case 6: month=6;break;
            case 7: month=7;break;
            case 8: month=8;break;
            case 9: month=9;break;
            case 10: month=10;break;
            case 11: month=11;break;
        }
    }
    
     void setCalendar(){
      getYear();
      getMonth();
      int  count=1;
      cal.set(year, month, 1);    
      int start=7+cal.get(Calendar.DAY_OF_WEEK);
      int numbOfDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        
      for(int i=0;i<numbOfDay;i++){
        button[start].setText(String.valueOf(count)); 
        count++;
        start++;
        /*menambahkan action listener*/
        final int select=start;
        button[start].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                dayclicked = button[select].getActionCommand();
                result=String.valueOf(year+"-"+(month+1)+"-"+dayclicked);           
                dispose();
            }
        });
      }
      datetime.setText(cBulan.getSelectedItem().toString()+"-"+year);
    }
     
    public String hasil(){
       return result;
    }
    
    void cleanDate(){
        for(int i=8;i<button.length;i++){
            button[i].setText("");
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cBulan = new javax.swing.JComboBox();
        panel = new javax.swing.JPanel();
        datetime = new javax.swing.JLabel();
        fTahun = new javax.swing.JLabel();
        next = new javax.swing.JButton();
        back = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Kalender");
        setResizable(false);

        cBulan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "Nopember", "Desember" }));
        cBulan.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cBulanItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 212, Short.MAX_VALUE)
        );

        fTahun.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        fTahun.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        fTahun.setText("2013");

        next.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/resultset_next.png"))); // NOI18N
        next.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nextMouseClicked(evt);
            }
        });

        back.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/resultset_previous.png"))); // NOI18N
        back.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                backMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cBulan, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(back, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fTahun, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(next, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(datetime, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(next, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(back, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fTahun, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(datetime, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(cBulan, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cBulanItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cBulanItemStateChanged
        // TODO add your handling code here:
        getMonth();
        cleanDate();
        setCalendar();
    }//GEN-LAST:event_cBulanItemStateChanged

    private void backMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_backMouseClicked
        // TODO add your handling code here:
        int th = Integer.parseInt(fTahun.getText());
        th--;
        fTahun.setText(String.valueOf(th));
        getYear();
        getMonth();
        cleanDate();
        setCalendar();
    }//GEN-LAST:event_backMouseClicked

    private void nextMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nextMouseClicked
        // TODO add your handling code here:
        int th = Integer.parseInt(fTahun.getText());
        th++;
        fTahun.setText(String.valueOf(th));
        getYear();
        getMonth();
        cleanDate();
        setCalendar();
    }//GEN-LAST:event_nextMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Kalender.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Kalender.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Kalender.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Kalender.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Kalender().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton back;
    private javax.swing.JComboBox cBulan;
    private javax.swing.JLabel datetime;
    private javax.swing.JLabel fTahun;
    private javax.swing.JButton next;
    private javax.swing.JPanel panel;
    // End of variables declaration//GEN-END:variables
}
